<?php
namespace addons\upyunup;


use addons\upyunup\service\Provide;

class Upyunup extends \think\Addons
{
    //是否有后台
    public $hasAdmin = 1;
    // 该插件的基础信息
    public $info = [
        //插件英文名，改成你的插件英文就行了
        'name'        => 'Upyunup',
        //插件名称
        'title'       => '又拍云上传插件',
        //插件描述
        'description' => '又拍云上传插件',
        //插件，1:开启;0:禁用
        'status'      => 1,
        //插件作者
        'author'      => '辰一',
        //版本号
        'version'     => '1.0',
        //演示地址
        'demo_url'    => 'http://lve123.com',
        //作者网站链接
        'author_url'  => 'http://lve123.com',
        //是否有后台 0 没有，1有
        'has_admin'   => 1
    ];
    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $storageOption = get_option('storage');
        if (empty($storageOption)) {
            $storageOption = [];
        }
        //driver 驱动
        $storageOption['storages']['Upyunup'] = ['name' => '又拍云存储', 'driver' => '\\addons\\upyunup\\service\\Provide'];
        set_option('storage', $storageOption);
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        $storageOption = get_option('storage');
        if (empty($storageOption)) {
            $storageOption = [];
        }
        unset($storageOption['storages']['Upyunup']);
        set_option('storage', $storageOption);
        return true;
    }

    /**
     * 实现的uphook钩子方法
     * @return mixed
     */
    public function upyunhook()
    {
        $res = Provide::upload();
        if($res)
        {
            echo json_encode([
                "code"=> 1,
                "msg" => 'success',
                'data'=> $res
            ]);
        }else{
            echo json_encode([
                "code"=> 0,
                "msg" => 'error:缺少filed字段',
                'data'=> ''
            ]);
        }
    }

    /**
     * 实现的uphook钩子方法
     * @return mixed
     */
    public function upydownhook($param=["src"=>'','loca'=>false])
    {
        if(!isset($param['loca'])) $param['loca']=false;
        $res = Provide::downloadFile($param['src'],$param['loca']);
    }

    public function fetchUploadView()
    {
        $tab = request()->param('tab');
        if ($tab == 'cloud') {
            return $this->fetch("upyunup");
        } else {
            $content = "has_cloud_storage";
        }
        return $content;
    }

}